package com.sinosoft.web.easyui.api;

import com.sinosoft.hydra.um.LoginUser;
import cn.com.sinux.spring.commons.OrderCollection;
import com.sinosoft.hydra.um.condition.SearchMessageCondition;
import com.sinosoft.hydra.um.domain.permission.TMessage;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.MessageService;
import cn.com.sinux.spring.utils.PageUtils;
import cn.com.sinux.spring.utils.QueryHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 张元第创建
 * 消息接收的API
 */
@Controller
@RequestMapping("/api/messageManage")
public class SysMessageManageApi {


    @Autowired
    private MessageService messageService;

    /**
     * 消息列表页面
     *
     * @return 消息列表页
     */
    @RequestMapping("index")
    @RequiresPermissions("message:manage:list")
    public String index(LoginUser loginUser, Model model) {
        LoggerHelper.logInfo("查询了消息列表");
        return "hydra_easyui/message/manage/index";
    }

    /**
     * 消息管理页面
     *
     * @return 消息管理页面
     */
    @RequestMapping("toAdd")
    @RequiresPermissions("message:data:add")
    public String toAdd(LoginUser loginUser, Model model) {
        return "hydra_easyui/message/addMessage";
    }

    /**
     * 消息列表
     *
     * @param searchMessageCondition 删除消息的集合
     * @return 删除结果
     */
    @RequestMapping(value = "deleteMessages", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("message:data:delete")
    public Object deleteMessages(SearchMessageCondition searchMessageCondition) {
        List<TMessage> messages = messageService.deleteMessages(searchMessageCondition.getIds());
        List<String> names = QueryHelper.doQueryProperties(messages, true, new QueryHelper.PropertyReader<TMessage, String>() {

            public String read(TMessage message) {
                return message.getSendTitle();
            }
        });
        LoggerHelper.logInfo("删除了消息{}", messages);
        return true;
    }

    /**
     * 消息列表
     *
     * @param searchMessageCondition 删除消息的集合
     * @return 删除结果
     */
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNumber,
                       @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize,
                       SearchMessageCondition searchMessageCondition) {
        Pageable pageable = PageUtils.buildPageRequest(pageNumber, pageSize,
                new OrderCollection().addOrder(Sort.Direction.DESC, "createTime")
                        .addOrder(Sort.Direction.DESC, "id"));
        return messageService.findMessages(searchMessageCondition, pageable);
    }
}
